package com.bsh.serviceImpl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bsh.dto.CloudGoodsListQryDto;
import com.bsh.mybatis.dataView.UserAccountInfo;
import com.bsh.mybatis.model.Account;
import com.bsh.mybatis.model.AccountNotice;
import com.bsh.mybatis.model.Share;
import com.bsh.mybatis.model.User;
import com.bsh.repository.DaoAccount;
import com.bsh.repository.DaoAccountLog;
import com.bsh.repository.DaoAccountNotice;
import com.bsh.repository.DaoCloudGoods;
import com.bsh.repository.DaoShare;
import com.bsh.repository.DaoUser;
import com.bsh.service.ShareService;
import com.bsh.utils.Pager;

@Service
@Transactional
public class ShareServiceImpl implements ShareService {

	@Autowired
	DaoShare daoShare;
	@Autowired
	DaoAccountLog daoAccLog;
	@Autowired
	DaoAccount daoAcc;
	@Autowired
	DaoAccountNotice daoNotice;
	@Autowired
	DaoUser daoUser;
	@Autowired
	DaoCloudGoods daoCloudGoods;
	
	
	@Override
	public Integer saveShare(Share share) {
		return daoShare.insert(share);
	}

	@Override
	public Integer sumTodayCloudMoney(Share share) {
		return daoShare.sumTodayTotal(share);
	}

	@Override
	public Integer shareAddCloudMoney(int accountId,int userid,String sn,String ip,BigDecimal cloudMoney) {

		// 账户变动前记录日志
		daoAccLog.addLog(accountId, sn, ip);
		// 加现金
		daoAcc.addCloudMoney(accountId, userid, cloudMoney);
		UserAccountInfo endAccInfo = getUserAccountInfo(userid);
		// 写资金日志明细
		AccountNotice notice = new AccountNotice();
		notice.setAccountId(accountId);
		notice.setUserid(userid);
		notice.setCloudMoney(cloudMoney);
		notice.setTradeType(17); // 分享返商币
		notice.setSn(sn);// 流水号
		notice.setOuterTime(new Date());
		notice.setOperator(userid);// 操作人

		notice.setTitle(String.format("分享,得到商币: %s ,操作人:%s",
				cloudMoney, endAccInfo.getName()));
		//notice.setMemo(accChangeInfo.getMemo());
		notice.setPaymentId(3);// 系统自动操作
		notice.setIp(ip);
		notice.setIsPaid((byte) 1);// 已付款
		notice.setMoneyType((short) 0);// 商币
		notice.setCreateTime(new Date());
		notice.setCloudMoneyDown(endAccInfo.getCloudMoney()); // 结存商币余额
		return daoNotice.insert(notice);
	}

	public UserAccountInfo getUserAccountInfo(int userid) {
		UserAccountInfo userAcc = null;
		User user = daoUser.selectByPrimaryKey(userid);
		if (user == null)
			return userAcc;

		userAcc = new UserAccountInfo();
		userAcc.setName(user.getName());
		userAcc.setEnabled(user.getIsEffect());
		userAcc.setUserid(user.getId());

		Account acc = daoAcc.getUserAccount(userid);
		BigDecimal money = null, cloudMoney = null;
		if (acc != null) {
			money = acc.getMoney() == null ? BigDecimal.ZERO : acc.getMoney();
			cloudMoney = acc.getCloudMoney() == null ? BigDecimal.ZERO : acc.getCloudMoney();
			userAcc.setAccountId(acc.getId());
		}
		if (acc == null) {
			money = cloudMoney = BigDecimal.ZERO;
		}
		userAcc.setMoney(money);
		userAcc.setCloudMoney(cloudMoney);
		return userAcc;
	}

	@Override
	public List<Map<String, Object>> getGoodsPage(Pager pager, CloudGoodsListQryDto qryParam) {
		StringBuilder sb = new StringBuilder();
//		if (!StringUtils.isEmpty(qryParam.getMerchantName())){
//			String mName=qryParam.getMerchantName().trim();		
//			sb.append(String.format(" and(m.name = '%s' or m.merchant_name like '%%%s%%')", mName,mName));
//		}
//		if (!StringUtils.isEmpty(qryParam.getName())){
//			sb.append(String.format(" and g.name like '%%%s%%'", qryParam.getName().trim()));
//		}
//			
//		if (!StringUtils.isEmpty(qryParam.getInStore())&&qryParam.getInStore()!=-1){
//			sb.append(String.format(" and g.in_store = %s", qryParam.getInStore()));
//		}
			
		List<Map<String, Object>> list=daoCloudGoods.getGoodsPage(pager, sb.toString());
		return list;
	}
	
}
